$(function(){
    var total = 1;//分页总页面数
    var total_count = 1;//分页总记录数
    var currentPage = 1;//当前页
    var pageSize = pageOption.pageSize;//每页显示的记录数

    function init(){
        myPagination();
        bindEvent();
    }

    function bindEvent(){

        /**
         * 模糊查询事件
         *
         */
        $(".inner-section .search-area .search-btn").click(function(){
           render(true,1,pageSize);
        });
        //enter事件
        $(".inner-section .search-area input").keydown(function(event){
            event = event ? event:window.event;
            if(event.keyCode == 13){
                render(true,1,pageSize);
            }
        });

        //查看用户详情

        //删除
        $(".inner-section .delete").click(function(){
            var id = $(this).attr("data-id");
            myConfirmModal("确定删除吗？",function(){
                $.ajax({
                    url:"deleteUser",
                    type:"post",
                    data:{"id":id},
                    dataType:"json",
                    beforeSend:function(xhr){
                        //显示“加载中。。。”
                        $("#loading").modal('show');
                    },
                    complete:function(){
                        //隐藏“加载中。。。”
                        $("#loading").modal('hide');
                    },
                    success:function(json,statusText){
                        if(json.errorCode == 0){
                            if(currentPage !=1 && total_count % pageSize == 1){//非首页且末页记录数为1时
                                currentPage = currentPage - 1;
                            }
                            render(true,currentPage,pageSize);
                        }else{
                            responseTip(json.errorCode,json.errorInfo,1500);
                        }
                    },
                    error:errorResponse
                });
            });

        });
    }
	
	function black(){
		 var id = $(this).attr("data-id");
		 var black = $(this).attr("data-type");
		 var notice = "";
		 if(black == 1){
		 	notice = "确定要停用该用户吗？";
		 }else{
		 	notice = "确定要启用该用户吗？";
		 }
        myConfirmModal(notice,function(){
            $.ajax({
                url:"isBlack",
                type:"post",
                data:{"user_id":id,"isBlack":black},
                dataType:"json",
                beforeSend:function(xhr){
                    //显示“加载中。。。”
                    $("#loading").modal('show');
                },
                complete:function(){
                    //隐藏“加载中。。。”
                    $("#loading").modal('hide');
                },
                success:function(json,statusText){
                    if(json.errorCode == 0){
                        if(currentPage !=1 && total_count % pageSize == 1){//非首页且末页记录数为1时
                            currentPage = currentPage - 1;
                        }
                        render(true,currentPage,pageSize);
                    }else{
                        responseTip(json.errorCode,json.errorInfo,1500);
                    }
                },
                error:errorResponse
            });
        });
		
	}
		
	
          
    
	
    /**
     * 查看用户详情
     */
    function userDetail(){
        var id = $(this).attr("data-id");
        window.location.href = "userDetail?id="+id;
    }

   
    /**
     * 案例分页显示方法
     */
    function myPagination(){
        render(true,1,pageSize);
        //调用公共分页方法
        pagination("#page-selection",{pageSize:pageSize,total:total},render);

    }
    /**
     * 获取模糊参数
     */
    function getSelectInfo(){
        var name = $.trim($(".search-area .name").val());
        var phone = $.trim($(".search-area .phone").val());
        var selectInfo = {
            "name":name,
            "phone":phone,
        };
        return selectInfo;
    }
    /**
     * 分页动态渲染数据
     * @param async ajax请求是否异步
     * @param pageIndex 当前显示页
     * @param pageSize 每页显示记录数
     */
    function render(async,pageIndex,pageSize){
        var selectInfo = getSelectInfo();
        selectInfo.pageIndex = pageIndex;
        selectInfo.pageSize = pageSize;
        $.ajax({
            async:async,
            type:'post',
            url:'pagingUser',
            data:selectInfo,//从1开始计数
            dataType:'json',
            success:function(result){
                var html ='';
                if(result.errorCode == 0){
                    total = result.data.pageInfo.total_page;
                    total_count = result.data.pageInfo.total_count;
                    $("#page-selection").bootpag({total:total,total_count:total_count});//重新计算总页数,总记录数

                    currentPage = result.data.pageInfo.current_page;
                    var userList = result.data.dataList;

                    html+='<tr><th class="th1">序号</th><th class="th2">头像</th><th class="th6">昵称</th><th class="th6">姓名</th><th class="th6">手机号</th><th class="th5">性别</th><th class="th6">地区</th><th class="th10">添加时间</th><th class="th11">操作</th></tr>';
                    var colspan = $(html).find("th").length;
                    for(var i = 0; i < userList.length;i++){
                        var obj = userList[i];
                        var number = (pageIndex - 1)*pageSize + i + 1;//序号
                        var head_url = obj.head_img_url;
                        var name = obj.name || '--';
                        var phone = obj.phone || '--';
                        var nick_name = obj.nick_name || '--';
                        var address = obj.country+'-'+obj.province+'-'+obj.city;
                        var sex = obj.sex;
                        var add_time = obj.add_time || '--';
                        var sex_text = '未知';
                        if(sex == '1'){
                        	sex_text = '男';
                        }else{
                        	sex_text = '女';
                        }
                        var is_black = obj.is_black;
                        var black_text = "";
                        var id = obj.id;
                        if(is_black == 0){
                        	black_text = '<a href="javascript:;" data-id="'+id+'" data-type="1" class="btn btn-xs btn-default  black">停用</a>';
                        }else{
                        	black_text = '<a href="javascript:;" data-id="'+id+'" data-type="0"  class="btn btn-xs btn-primary  black">启用</a>';
                        	
                        }
                        
                        html+='<tr>'
                        		+'<td>'+number+'</td>'
                        		+'<td>'+(head_url?'<img width="50" src="'+head_url+'">':'无头像')+'</td>'
                        		+'<td><span class="limit-text" title="'+nick_name+'">'+nick_name+'</span></td>'
                        		+'<td>'+name+'</td>'
                        		+'<td>'+phone+'</td>'
                        		+'<td>'+sex_text+'</td>'
                        		+'<td>'+address+'</td>'
                        		+'<td>'+add_time+'</td>'
                        		+'<td>'
                        			+(getValue.enable() == 1 ? black_text:'') 
                        			+(getValue.isSee() == 1 ? '<a href="javascript:;" class="user-detail btn btn-xs btn-primary" data-id="'+id+'">查看</a>' : '')
                        		+'</td>'
                    		+'</tr>';
                    }
                    if(userList.length == 0){
                        html += '<tr><td colspan="'+colspan+'"><p class="text-danger">暂无数据。</p></td></tr>';
                    }
                    $(".inner-section #list-table tbody").html(html);
                    $(".user-detail").click(userDetail);
                  
                    $(".black").click(black);

                }else{
                    responseTip(result.errorCode,json.errorInfo,1500);
                }

            },
            error:errorResponse
        });
    }
    init();
});